package br.com.ifba.sadd.admin.presentation.modalidade {
	import br.com.ifba.sadd.model.ModelLocator;
	import br.com.ifba.sadd.model.ServiceLocator;

	import mx.controls.Alert;
	import mx.events.ValidationResultEvent;
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	[Event(name="reset", type="flash.events.Event")]

	[Bindable]
	public class AlterarModalidadePresentationModel extends EventDispatcher
	{			
		private var _enable:Boolean = false;
		private var _model:ModelLocator = ModelLocator.getInstance();
		private var _service:ServiceLocator = ServiceLocator.getInstance();
			
		public function AlterarModalidadePresentationModel()
		{
		}
		
		/* ---------------------- Geters e Seters ----------------------- */
		
		public function get enable():Boolean
		{
			return _enable;
		}
		
		public function set enable(value:Boolean):void
		{
			_enable = value;
		}
		
		public function get model():ModelLocator
		{
			return _model;
		}
	
		/* ---------------------------- Crud ---------------------------- */
		
		public function update(value:String):void
		{
			model.modalidade.nome = value.toUpperCase();			
			var token:AsyncToken = _service.modalidadeService.alterarModalidade(model.modalidade);
			token.addResponder(new Responder(handleEdit, handleFault));
		}
		
		/* ---------------------------- Apoio --------------------------- */
			
			public function validarCadastro(value:ValidationResultEvent):void
			{
				if(value.type == ValidationResultEvent.VALID)
				{
					enable = true;
				}
				else
				{
					enable = false;
				}
			}
		/* ---------------------------- Handles ------------------------- */
		
		private function handleEdit(event:ResultEvent):void
		{
			Alert.show("Cadastro Realizado!");
			dispatchEvent(new Event("reset"));
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show(event.fault.faultString);
		}
	}
}
